<?php
	include_once "dataservice.php";
	
	class submenu_db
	{
		public function Them($MaMenu, $MaChuyenMuc, $TenSubMenu, $Link, $Index)
		{
			$sql = "INSERT INTO `tbl_submenu`(`MaMenu`, `MaChuyenMuc`, `TenSubMenu`, `Link`, `Index`) 
					VALUES ('$MaMenu', '$MaChuyenMuc', '$TenSubMenu', '$Link', '$Index')";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhat($MaSubMenu, $MaMenu, $MaChuyenMuc, $TenSubMenu, $Link, $Index)
		{
			$sql = "UPDATE 	`tbl_submenu` 
					SET 	`MaMenu`='$MaMenu',
							`MaChuyenMuc`='$MaChuyenMuc',
							`TenSubMenu`='$TenSubMenu',
							`Link`='$Link',
							`Index`='$Index' 
					WHERE 	`MaSubMenu`='$MaSubMenu'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatPIN($MaSubMenu, $PIN)
		{
			$sql = "UPDATE 	`tbl_submenu` 
					SET 	`PIN`='$PIN'
					WHERE 	`MaSubMenu`='$MaSubMenu'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatTrangThai($MaSubMenu, $TrangThai)
		{
			$sql = "UPDATE 	`tbl_submenu` 
					SET 	`TrangThai`='$TrangThai'
					WHERE 	`MaSubMenu`='$MaSubMenu'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function Xoa($MaSubMenu)
		{
			$sql = "DELETE FROM `tbl_submenu` WHERE `MaSubMenu` = '$MaSubMenu'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function LayDanhSach($MaSubMenu, $MaMenu, $MaChuyenMuc, $Index, $TrangThai)
		{
			$sql = "SELECT * FROM `tbl_submenu` WHERE 1";
			
			if($MaSubMenu != 0)
				$sql .= " AND `MaSubMenu` = '$MaSubMenu'";
			if($MaMenu != 0)
				$sql .= " AND `MaMenu` = '$MaMenu'";
			if($MaChuyenMuc != 0)
				$sql .= " AND `MaChuyenMuc` = '$MaChuyenMuc'";
			if($Index != 0)
				$sql .= " AND `Index` = '$Index'";
			if($TrangThai != -1)
				$sql .= " AND `TrangThai` = '$TrangThai'";
				
			$sql .= " ORDER BY `Index`";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		public function MaxIndex($MaMenu)
		{
			$sql = "SELECT MAX(`Index`) FROM `tbl_submenu` WHERE `MaMenu` = '$MaMenu'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
	}
?>